package com.mulanweb.handler.wechat;

import com.mulanweb.api.module.model.*;
import com.mulanweb.db.common.Constant;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimerTask;

/**
 * Created by Administrator on 2016/8/30.
 */
public class QuerySellerPayment extends TimerTask {

    @Override
    public void run() {
        List<Order> orders = Order.dao.find("SELECT * FROM `order` WHERE status_id IN (4,7,8) AND verify_code_status =15 AND type_id = 24");
        //PaymentSeller.paymentSeller("XXL07147874539763015","oULNBwOvt_D2A_8se1bQOQ06ky0w","曾德仁",Integer.parseInt((1*100)+""));
        for (Order order : orders) {
            /*if (order.getTypeId() == 23 && order.getStatusId() == 4L) {
                try {
                    Date createTime = order.getCreateTime();
                    Date date = new Date();//获取当前时间
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(createTime);
                    calendar.add(calendar.DATE, Constant.XinXiLan.VERIFY_CODE_OVERTIME);//把日期往后增加一天.整数往后推,负数往前移动
                    Date overTime = calendar.getTime();   //这个时间就是日期往后推一天的结果
                    //验证码超过三十天，已过期
                    if (date.getTime() <= overTime.getTime()){
                        PaymentSeller.refundToUser(order.getOrderNumber(), order.getAmount()*100);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else */if (order.getTypeId() == 24 && (order.getStatusId() == 7 || order.getStatusId() == 8)) {
                if (order.getVerifyCodeStatus() == 15) {
                    Reserve reserve = Reserve.dao.findFirst("SELECT * FROM reserve WHERE user_id=? AND driver_id AND chartered_id AND end_time <= now()");
                    if (reserve != null && reserve.getId() != null) {
                        if (CheckOrderPayStatus.checkPayStatus(order.getOrderNumber())) {
                            SellerVerify sellerVerify = SellerVerify.dao.findFirst("SELECT * FROM seller_verify WHERE seller_id=?", order.getSellerId());
                            Seller seller = Seller.dao.findById(order.getSellerId());
                            if (sellerVerify != null) {
                                Chartered chartered = Chartered.dao.findById(order.getTargetId());
                                PaymentSeller.paymentSeller(order.getOrderNumber(), sellerVerify.getOpenId(), seller.getSellerName(),
                                        //单位为元 转换成分，1-利润折扣
                                        Integer.parseInt((order.getAmount() * order.getUnitPrice() * 100 * (1 - chartered.getProfit() / 100)) + ""));
                            }
                        }
                    }
                }
            }
        }
    }
}
